package xing.application.scheduler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import xing.application.service.isv.suite.SuiteTokenService;
import xing.domain.isv.suite.Suite;
import xing.repository.isv.suite.SuiteRepository;

import java.util.List;

/********************************
 * 文 件 名 :SuiteTokenGenerateJob.java
 * 创 建 人：陈天龙
 * 日    期：2017/8/23
 * 描   述：生成或更新suite访问开放平台接口token定时任务
 * 版 本 号：v1.0.0
 ********************************/
@Component
public class SuiteTokenGenerateJob {

    @Autowired
    private SuiteRepository suiteRepository;
    @Autowired
    private SuiteTokenService suiteTokenService;

    private final Logger logger = LoggerFactory.getLogger(SuiteTokenGenerateJob.class);

    @Scheduled(cron = "${spring.scheduling.corn}")
    public void excuteSuiteTokenJob(){
        try {
            logger.info("准备执行suiteToken的定时任务");
            //获取所有的套件
            List<Suite> suites = suiteRepository.findAll();
            if(null!=suites && !CollectionUtils.isEmpty(suites)) {
                suites.stream().forEach(suite -> {
                    try {
                        suiteTokenService.saveOrUpdateSuiteToken(suite.getSuiteKey());
                        logger.info("saveOrUpdateSuiteToken成功");
                    } catch (Exception e) {
                        logger.error("saveOrUpdateSuiteToken失败", e);
                    }
                });
            }
        } catch (Exception e) {
            logger.error("执行定时任务失败",e);
        }

    }

}
